import psutil as pl
import paramiko as po
import re

def ssh_connect(host,user,passwd,cmd,port=22):#连接远程主机
	s = po.SSHClient()	#初始化ssh
	s.set_missing_host_key_policy(po.AutoAddPolicy())#将主机添加到know_hosts文件中
	s.connect(host,port,user,passwd)#连接主机
	stdin,stdout,stderr = s.exec_command(cmd)#执行终端命令
	cmd_result = stdout.read()#,stderr.read()#读取执行结果
	s.close()#关闭连接
	return cmd_result


def code_cvrsion(self):		#字节码转字符串
	str = ''
	for i in self:
		str += chr(i)
	return str

def local_run_time():#计算本地系统运行时间
	fe = open('/proc/uptime',mode='r')
	time = fe.readline()
	str1 = ''
	i = 0
	d = 86400#1天=86400秒,1小时=3600秒
	h = 3600
	s = 60
	fe.close()
	while i<int(len(time)):
		if time[i] == ' ':
			break
		else:
			s = str1 + time[i]
			i += 1
			print(str1)
	k = float(str1)
	print('系统已运行:{}天{}小时{}分{}秒'.format(int(k/d),int((k%d)/h),int((k%h)/s),int(k%s)))

def tel_run_time(self):#获取远程主机运行时间
	d = 86400#1天=86400秒,1小时=3600秒
	h = 3600
	s = 60
	v=self.find(' ')
	str=self[:v]
	ft = float(str)
	print('系统已运行:{}天{}小时{}分{}秒'.format(int(ft/d),int((ft%d)/h),int((ft%h)/s),int(ft%s)))

def sys_type(self):#获取远程主机系统版本
	i=len(var)
	j=0
	k=0
	str1=''
	while j < i:
		if var[j] == 9:
			k += 1
			if k == 2:
				v = j + 1
				break
		j += 1
	while v < i:
		if var[v] == 10:
			break
		else:
			s1 = chr(var[v])
			str1 += s1
		v += 1
	print('系统版本:{}'.format(str1))

def dk_info(self):	#获取远程主机磁盘信息
	#print(self)
	v = len(self)
	k = i = 0
	str1=self
	'''
	str1 = ''
	while k < v:
		str1 += chr(var[k])
		k += 1'''
	
	r = '/dev/[a-z]+[\d]*[\S]*[\s]+[\w]+[\s]+[\d]+[\.]?[\d]+[GMK]*[\s]+[\d]+[\.]*[\d]+[GMK]*[\s]+[\d]+[\.]*[\d]*[GMK]*[\s]+[\d]+[%]+[\s]+/+[a-z]*/*[\w]*'	#匹配磁盘信息
	str2 = re.findall(r,self)
	#print('str2:\n{}'.format(str2))
	dk_name = '/dev/[a-z]+[\d]*[\S]?'		#匹配磁盘名称
	fe_sys = 'n[t]*fs*|ext[234]*|vfs*|jfs2*|xfs*|swap*|vfat*|iso9660*'#匹配文件系统类型
	dk_size = '[\d]+[\.]?[\d]+[GMK]+|\s0\s'	#匹配磁盘总容量,已使用,剩余
	ud_pcae = '[\d]+[%]+'					#匹配磁盘使用百分比
	mt_dir = '/+[\S]*'#'/+[\w]*/*[\w]*'				#匹配磁盘名和挂载目录
	#print('值:{}\n类型:{}'.format(str2,type(str2)))
	print('磁盘名称\t文件系统 总容量\t已使用\t剩余  已使用%\t挂载目录')
	for str3 in str2:
		#print('值:{}\n类型:{}'.format(str3,type(str3)))
		#print(str3[2])
		fe = re.findall(fe_sys,str3)
		dk = re.findall(dk_size,str3)#
		ud = re.findall(ud_pcae,str3)#匹配磁盘使用百分比
		mt = re.findall(mt_dir,str3)#匹配磁盘名和挂载点
		#print('{},{},{},{}'.format(fe,dk,ud,mt))
		#print('磁盘名称:{dk} 文件系统:{fs} 总容量:{ds_0} 已使用:{ds_1} 剩余:{ds_2} 已使用%:{up} 挂载目录:{md}'.format(dk=mt[i],md=mt[i+1],fs=fe[i],ds_0=dk[i],ds_1=dk[i+1],ds_2=dk[i+2],up=ud[i]))
		print('{dn}\t{fs}\t  {ds_0}\t {ds_1}\t{ds_2}\t{up}\t{md}'.format(dn = mt[i],md = mt[i+1],fs = fe[i],ds_0 = dk[i],ds_1 = dk[i+1],ds_2 = dk[i+2],up = ud[i]))

def memory_info(self):
	print(len(self))
	i=1000000
	v=re.findall('[\d]+',self)
	for j in v:
		float(j)
	print(float(v[0])/i)
	print(float(v[1])/i)
	print('\t总内存\t已使用\t空闲\t共享内存\t磁盘缓存\t可用\n物理内存  {0:.1f}G\t{1:.1f}G\t{2:.1f}G\t{3:.1f}G\t{4:.1f}G\t{5:.1f}G\n交换内存  {6:.1f}G\t{7:.1f}G\t{8:.1f}G'.format(float(v[0])/i,float(v[1])/i,float(v[2])/i,float(v[3])/i,float(v[4])/i,float(v[5])/i,float(v[6])/i,float(v[7])/i,float(v[8])/i))

#host = '193.112.152.28'
host = '192.168.11.129'		#129centos,144debian
port = 22
user = 'sshtt'
passwd = 'zxc.123'
cmd_1 = 'lsb_release -a'	#获取系统版本
cmd_1r = 'cat /etc/redhat-release'
cmd_1c = 'cat /etc/centos-release'
cmd_2 = 'cat /proc/uptime'	#获取系统运行时间
cmd_3 = 'df -hT'	#获取磁盘信息
cmd_4 = 'free'
cmd_5 = 'uname -r'	#获取内核版本
var = ssh_connect(host,port,user,passwd,cmd_4)
var = var.decode()#将字节码解码
print('类型:{}\t内容:\n{}'.format(type(var),var))
#sys_type(var)
#tel_run_time(var)
#dk_info(var)
#code_cvrsion(var)
memory_info(var)



#print('值:\n{}\n类型:{}'.format(str2,type(str2)))
#print(str2[0])
#f=open('2.txt','w')
#f.write(str2[0])
#f.close()














'''
var = pl.cpu_times()
#var = pl.cpu_percent(interval=1000,percpu=False)
print(len(var))
print(var)
sum=var[0]+var[1]+var[2]+var[3]+var[4]
print('%.2f'%sum)
print(sum)
'''











